import {cancelOrder, queryOrder} from "@src/api/catering";
import payBehavior from "../order/payBehavior";
import dayjs from "dayjs";
import $math from "@src/common/math";

Component({
  behaviors: [payBehavior],
  data: {
    order:null
  },
  methods: {
    onLoad(options) {
      let orderNo = options.orderNo;
      this.setData({orderNo: orderNo})
    },
    onLogin() {
      this.fetchData();
    },
    onPullDownRefresh() {
      this.fetchData();
      wx.stopPullDownRefresh();
    },
    fetchData() {
      let data = {orderNo: this.data.orderNo}
      queryOrder(data).then(res => {
        if (res.code === 0) {
          this.setData({order: res.data})
          this.setData({shopInfo: res.data.shopInfo});
          this.payTimeCountDown();
        }
      })
    },
    payTimeCountDown() {
      // 支付倒计时
      let createTime = dayjs(this.data.order.createTime, 'YYYY-MM-DD HH:mm');
      let endTime = createTime.unix() + 15 * 60
      let remainTime = endTime - dayjs().unix();
      this.setData({remainTime: remainTime, endTime})
      let orderStatus = this.data.order.orderStatus;
      if (remainTime >= 0 && (orderStatus === 2 || orderStatus === 1)) {
        this.cutTime(remainTime);
      }
    },
    // 倒计时
    cutTime(remainTime) {
      let minute = Math.floor(remainTime / 60);
      let second = remainTime % 60;
      if (this.data.timer) {
        clearInterval(this.data.timer);
      }
      let timer = setInterval(() => {
        let remainTime = this.data.endTime - dayjs().unix();
        if (remainTime <= 0 && this.data.timer) {
          clearInterval(this.data.timer);
          this.setData({timer: null, remainTime: 0})
          this.fetchData();
          return;
        }
        let minute = Math.floor(remainTime / 60);
        let second = remainTime % 60;
        this.setData({remainTime: remainTime, remainTimeM: minute, remainTimeS: second})
      }, 1000);
      this.setData({remainTime: remainTime, remainTimeM: minute, remainTimeS: second, timer: timer})
    },
    handleToPay() {
      let payChannels = [];
      if (this.data.shopInfo.payTypes.includes(3)) {
        payChannels.push("balance")
      }
      if (this.data.shopInfo.payTypes.includes(1)) {
        payChannels.push("wx")
      }
      let orderNo = this.data.order.orderNo
      let params = {
        orderNo: orderNo,
        payFee: $math.mul(this.data.order.payFee, 100, 0),
        hasDiscount: this.data.order.discountFee > 0,
        payChannels: payChannels
      }
      this.toCateringPay(params).then(() => {
        wx.showToast({
          title: "支付成功",
          icon: "success",
          duration: 2000
        });
        this.fetchData();
      }).catch((err) => {
        console.info("error", err)
      });
    },
    handleCancelOrder() {
      let data = {
        orderNo: this.data.orderNo
      };
      wx.showModal({
        title: "",
        content: "要取消订单吗？",
        cancelText: "点错了",
        confirmText: "是的",
        confirmColor: "#F56C6C",
        success: (res) => {
          if (res.confirm) {
            wx.showLoading({title: "订单取消中..."});
            cancelOrder(data).then(res => {
              wx.hideLoading()
              if (res.code === 0) {
                wx.showToast({
                  title: "取消订单成功",
                  icon: "success",
                  duration: 2000
                });
              }
              this.fetchData();
            }).catch(() => {
              wx.hideLoading()
            })
          }
        }
      })
    }
  }
})
